package kr.couponplay.android.activity.deal.list;

import java.util.List;

import kr.couponplay.android.AppContext;
import kr.couponplay.android.activity.deal.DealDetailActivity;
import kr.couponplay.android.domain.BeanFactory;
import kr.couponplay.android.domain.deal.Deal;
import kr.couponplay.android.domain.deal.DealDao;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;

abstract public class DealListFragment extends Fragment implements OnItemClickListener, OnScrollListener {
	protected ProgressDialog dataLoadindDialog;
	protected DealListViewAdapter dealListViewAdapter;
	protected ListView dealListView;
	protected int lastSize;
	protected boolean isAttachLock = false;
	protected boolean isMoreView = false;
	protected LinearLayout dealListHeaderView;
	protected LinearLayout dealListFooterView;
	protected List<Deal> deals;
	
	
	abstract public int getPosition();
	abstract public int getDealsSize();
	abstract public void getDataFromServer(Context context);
	abstract public void drawBodyWhenInit();
	
	public void drawHeader() {
		dealListView.addHeaderView(dealListHeaderView);
	}
	
	public void drawBodyWhenMore(List<Deal> newDeals) {
		for (Deal each : newDeals) {
			dealListViewAdapter.add(each);
		}
		dealListViewAdapter.notifyDataSetChanged();
	}
	
	public void drawFooter(Context context, int currCount) {
		if (currCount < AppContext.getDealListLimitCount(context)) {
			dealListView.addFooterView(dealListFooterView);
		}
	}	
	
	public DealListViewAdapter getDealListAdapter() {
		return dealListViewAdapter;
	}
	
	protected void changePosition(final int position) {
		dealListView.clearFocus();
		dealListView.post(new Runnable() {
		    @Override
		    public void run() {
		    	dealListView.requestFocusFromTouch();
		    	dealListView.setSelection(position);
		    	dealListView.requestFocus();
		    }
		});
	}
	
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		Deal deal = dealListViewAdapter.getItem(position - dealListView.getHeaderViewsCount());
		addClickDeal(view.getContext(), deal.getId());
		Intent intent = new Intent(view.getContext(), DealDetailActivity.class);
		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		intent.putExtra("deal", deal);
		startActivity(intent);
	}
	
	public void addClickDeal(final Context context, final int dealId) {
		Thread thread = new Thread(new Runnable() {
            public void run() {
            	try {
	        		BeanFactory.getBean(context.getApplicationContext(), DealDao.class).addClickDeal(dealId);
            	} catch (Exception e) {
            	}
            }
        });
        thread.start();
	}
	
	@Override
	public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
		if (isAttachLock) {
	    	return;
	    }
		
		if (dealListView.getFooterViewsCount() > 0) {
			return;
		}
		
		if (totalItemCount > 0 && firstVisibleItem + visibleItemCount == totalItemCount) {
			isMoreView = true;
			getDataFromServer(view.getContext());
	    }  
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
	}	
}
